<?php

class PayModel extends Model{

    /**
     * 提币引用的方法
     * @param unknown $url 钱包地址
     * @param unknown $money 提币数量
     * 需要加密 *********************
     */
    public function wallet_withdraw($url,$money,$currency){
        if(!class_exists('Bitcoin')){
            Yaf\loader::import(APP_PATH.'/common/lib/easybitcoin.php');
        }
        $bitcoin = new Bitcoin($currency['rpc_user'],$currency['rpc_pwd'],$currency['rpc_url'],$currency['port_number']);
        $address = $bitcoin->validateaddress($url);
        if($address['isvalid']){
            $bitcoin->walletlock();//强制上锁
            $bitcoin->walletpassphrase($currency['rpc_key'],20);
            $id=$bitcoin->sendtoaddress($url,floatval($money));
            $bitcoin->walletlock();
            return $id;
        }else{
            return false;
        }
        
    }

    /**
     * 查询某人的交易记录
     * @param unknown $user 用户名
     * @param unknown $count  从第几个开始查找
     * @return $list  返回此用户的交易列表
     */
    public function trade_wallet($user,$currency){
        if(!class_exists('Bitcoin')){
            Yaf\loader::import(APP_PATH.'/common/lib/easybitcoin.php');
        }
        $bitcoin = new Bitcoin($currency['rpc_user'],$currency['rpc_pwd'],$currency['rpc_url'],$currency['port_number']);
        // $result = $bitcoin->getinfo();
        $list=$bitcoin->listtransactions($user,10,0);
        return $list;
    }
    
    
    /**
     * 获取新的一个钱包地址
     * @return unknown
     */
    public function new_address($member_id,$currency){
        if(!class_exists('Bitcoin')){
            Yaf\loader::import(APP_PATH.'/common/lib/easybitcoin.php');
        }
        $bitcoin = new Bitcoin($currency['rpc_user'],$currency['rpc_pwd'],$currency['rpc_url'],$currency['port_number']);
        $address = $bitcoin->getnewaddress('MEMBER_ID_'.$member_id);
        return $address;
    }

    /**
     * 检测地址是否是有效地址
     *
     * @return boolean 如果成功返回个true
     * @return boolean 如果失败返回个false；
     *  @param unknown $url
     *  @param $port_number 端口号 来区分不同的钱包
     */
    public function check_address($url,$currency){
        if(!class_exists('Bitcoin')){
            Yaf\loader::import(APP_PATH.'/common/lib/easybitcoin.php');
        }
        $bitcoin = new Bitcoin($currency['rpc_user'],$currency['rpc_pwd'],$currency['rpc_url'],$currency['port_number']);
        $address = $bitcoin->validateaddress($url);
        if($address['isvalid']){
            return true;
        }else{
            return false;
        }
    }

}